﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ww.wwf.wwfbll;
using ww.lis.lisbll.sam;
using System.Collections;
namespace ww.form.lis.sam.qc
{
    public partial class QC_VarSetForm : ww.form.wwf.SysBaseForm
    {
        ItemBLL bllItem = new ItemBLL();//项目逻辑        
        DataTable dtItem = new DataTable();//当前项目表    
        DataTable dtZKP = new DataTable();//当前项目表
        DataRowView rowCurrItem = null;//当前项目行
        string strCurrItemGuid = "";
        string strInstrID = "";//仪器ID
        string strFzkp_id = "";//质控品ID
        string strFzkp_qyrq = "";//质控品启用日期
        DataTable dtInstr = new DataTable(); //fjytype_id     
        InstrBLL bllInstr = new InstrBLL();

        DataTable dt参数 = new DataTable();

        public QC_VarSetForm()
        {
            InitializeComponent();
        }

        private void ItemSetForm_Load(object sender, EventArgs e)
        {
            GetInstrDT();
        }


        private void GetItemDT()
        {
            try
            {
                if (dtItem != null)
                    dtItem.Clear();
                dtItem = this.bllItem.BllItemZK(1, strInstrID);
                this.bindingSource项目.DataSource = dtItem;
            }
            catch (Exception ex)
            {
                WWMessage.MessageShowError(ex.ToString());
            }
        }

        private void GetZK_ZKP()
        {
            try
            {
                if (dtZKP != null)
                    dtZKP.Clear();
                dtZKP = this.bllItem.BllZKP(1, strInstrID);
                this.comboBox质控品.DataSource = dtZKP;
            }
            catch (Exception ex)
            {
                WWMessage.MessageShowError(ex.ToString());
            }
        }

        /// <summary>
        /// 参数
        /// </summary>
        private void GetVarDT()
        {
            try
            {
                string sql = "SELECT  *  FROM  SAM_ZK_SAMPLE where finstr_id='" + this.strInstrID + "' and fitem_id='" + strCurrItemGuid + "' and fzkp_id='" + strFzkp_id + "' order by fzk_date  ";
                dt参数 = ww.wwf.wwfbll.WWFInit.wwfRemotingDao.DbExecuteDataSetBySqlString(ww.wwf.wwfbll.WWFInit.strDBConn, sql).Tables[0];

                this.bindingSource参数.DataSource = dt参数;
            }
            catch (Exception ex)
            {
                WWMessage.MessageShowError(ex.ToString());
            }
        }


        /// <summary>
        /// 已经启用的仪器
        /// </summary>
        private void GetInstrDT()
        {
            try
            {
                if (dtInstr.Rows.Count > 0)
                    dtInstr.Clear();

                this.dtInstr = this.bllInstr.BllInstrDTByUseAndGroupID(1, ww.wwf.wwfbll.LoginBLL.strDeptID);//仪器列表
                this.comboBoxInstr.DataSource = dtInstr;
            }
            catch (Exception ex)
            {
                WWMessage.MessageShowError(ex.ToString());
            }
        }


        private void comboBoxInstr_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                strInstrID = this.comboBoxInstr.SelectedValue.ToString();
                GetZK_ZKP();
            }
            catch (Exception ex)
            {
                WWMessage.MessageShowError(ex.ToString());
            }
        }



        private void toolStripButtonS_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                this.Validate();
                this.bindingSource参数.EndEdit();
                this.DataGridViewObject.EndEdit();
                IList lisSql = new ArrayList();
                for (int i = 0; i < this.dt参数.Rows.Count; i++)
                {
                    StringBuilder strSql = new StringBuilder();
                    strSql.Append("update SAM_ZK_SAMPLE set ");
                    strSql.Append("fzkp_code='" + this.dt参数.Rows[i]["fzkp_code"].ToString() + "',");
                    strSql.Append("fld='" + this.dt参数.Rows[i]["fld"].ToString() + "',");
                    strSql.Append("fio_code='" + this.dt参数.Rows[i]["fio_code"].ToString() + "',");
                    if (this.dt参数.Rows[i]["fcv"] != null)
                        strSql.Append("fcv=" + this.dt参数.Rows[i]["fcv"] + ",");
                    if (this.dt参数.Rows[i]["fsd"] != null)
                        strSql.Append("fsd=" + this.dt参数.Rows[i]["fsd"] + ",");
                    if (this.dt参数.Rows[i]["fbz"] != null)
                        strSql.Append("fbz=" + this.dt参数.Rows[i]["fbz"] + ",");
                    if (this.dt参数.Rows[i]["fbc"] != null)
                        strSql.Append("fbc='" + this.dt参数.Rows[i]["fbc"] + "'");
                    strSql.Append(" where finstr_id='" + this.dt参数.Rows[i]["finstr_id"].ToString() + "' and fitem_id='" + this.dt参数.Rows[i]["fitem_id"].ToString() + "' and fzkp_id='" + this.dt参数.Rows[i]["fzkp_id"].ToString() + "' and fzk_date='" + this.dt参数.Rows[i]["fzk_date"].ToString() + "' ");

                    lisSql.Add(strSql.ToString());
                    
                }
                if (lisSql.Count > 0)
                {
                    string strRRR = WWFInit.wwfRemotingDao.DbTransaction(WWFInit.strDBConn, lisSql);

                    if (strRRR.Equals("true"))
                    {
                        WWMessage.MessageShowWarning("记录保存成功！");
                    }
                    else
                    {
                        WWMessage.MessageShowWarning(strRRR);
                    }

                    GetItemDT();
                }
            }
            catch (Exception ex)
            {
                WWMessage.MessageShowError(ex.ToString());
            }
            finally
            {
                Cursor = Cursors.Arrow;
            }
        }

        private void bll取得当前项目id()
        {
            try
            {
                rowCurrItem = (DataRowView)bindingSource项目.Current;//行
                if (rowCurrItem != null)
                {
                    strCurrItemGuid = rowCurrItem["fitem_id"].ToString();

                }
                else
                {
                    strCurrItemGuid = "";

                }
                this.GetVarDT();
            }
            catch (Exception ex)
            {
                WWMessage.MessageShowError(ex.ToString());
            }
        }
        private void bindingSource项目_PositionChanged(object sender, EventArgs e)
        {
            bll取得当前项目id();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            bll取得当前项目id();
        }

        private void toolStripButtonR_Click(object sender, EventArgs e)
        {
            this.GetVarDT();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("insert into SAM_ZK_SAMPLE(");
                strSql.Append("finstr_id,fitem_id,fzkp_id,fzk_date,fzkp_code,fld,fcv,fsd,fbz,fbc,fff,fsj_id,f1,f2,f3,f4,f5");
                strSql.Append(")");
                strSql.Append(" values (");
                strSql.Append("'" + this.strInstrID + "',");
                strSql.Append("'" + this.strCurrItemGuid + "',");
                strSql.Append("'" + this.strFzkp_id + "',");//fzk_date
                strSql.Append("'" + this.strFzkp_qyrq + "',");//fzk_date
                strSql.Append("'',");//fzkp_code
                strSql.Append("'高',");//fld
                strSql.Append("1,");//fcv
                strSql.Append("1,");//fsd
                strSql.Append("0,");//fbz
                strSql.Append("'',");//fbc
                strSql.Append("'',");//fff
                strSql.Append("'',");//fsj_id
                strSql.Append("'',");
                strSql.Append("'',");
                strSql.Append("'',");
                strSql.Append("'',");
                strSql.Append("''");
                strSql.Append(")");
                WWFInit.wwfRemotingDao.DbExecuteNonQueryBySqlString(WWFInit.strDBConn, strSql.ToString());
                this.GetVarDT();
            }
            catch (Exception)
            {
                WWMessage.MessageShowWarning("当前日期参数已经存在，请重新选择日期或质控品后重试。");
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            try
            {
                if (WWMessage.MessageDialogResult("确认要删除当前记录？"))
                {
                    StringBuilder strSql = new StringBuilder();
                    strSql.Append("delete SAM_ZK_SAMPLE ");
                    strSql.Append(" where finstr_id='" + this.strInstrID + "' and fitem_id='" + this.strCurrItemGuid + "' and fzkp_id='" + this.strFzkp_id + "' and fzk_date='" + this.strFzkp_qyrq + "' ");
                    WWFInit.wwfRemotingDao.DbExecuteNonQueryBySqlString(WWFInit.strDBConn, strSql.ToString());
                    this.GetVarDT();
                }
            }
            catch (Exception ex)
            {
                WWMessage.MessageShowWarning(ex.ToString());
            }
        }

        private void DataGridViewObject_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }

        private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }

        private void comboBox质控品_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (object.Equals(null, comboBox质控品.SelectedValue)) return;

            if (!object.Equals(null, dt参数)) dt参数.Rows.Clear();
            strFzkp_id = comboBox质控品.SelectedValue.ToString();
            string []zkxm = comboBox质控品.Text.Split('|');
            strFzkp_qyrq = zkxm[3];
            GetItemDT();
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            bll取得当前项目id();
        }

    }
}